package com.jinyang.jetpackdemo.activity

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.jinyang.jetpackdemo.R
import com.jinyang.jetpackdemo.bean.UserInfo

class KotlinsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlins)
//        initView()
        test2()
    }

    private fun test2() {
        //中缀调用
        val map = mapOf("key1" to 111, "key2" to 222)

        data class UserInfo(
            var name: String? = null,
            var age: Int = 0,
            var email: String? = null,
        )

        val user = UserInfo("ljy", 18, "xxx@qq.com")
        //解构声明
        //有时把一个对象看成一组单独的变量管理会变得更加简单
        //支持解构声明的对象的类必须是数据类(使用data关键字修饰的类,才会生成对应的component()方法)
        val (name1, age1, email1) = user
        log("name1=$name1,age1=$age1,email1=$email1")
        //可以使用下划线"_"省略不需要解构的属性
        val (name2, _, email2) = user
        log("name2=$name2,email2=$email2")

    }

    internal fun initView() {
        val buttonUserInfoSubmit: Button? = findViewById(R.id.btn_k1)
        buttonUserInfoSubmit?.text = "111"
        buttonUserInfoSubmit?.hint = "222"
        buttonUserInfoSubmit?.alpha = 0.5f
        buttonUserInfoSubmit?.setLines(1)
        //kotlin对于接口只有一个回调的方法的lambda简化
        buttonUserInfoSubmit?.setOnClickListener {
            doSomething()
        }
        //1. let
        //可以用it或自定义别名指代当前对象, 访问其公有的属性和方法, 比如原对象名称过于负责时
        //闭包形式返回
        buttonUserInfoSubmit.let { btn ->
            btn?.text = "111"
            btn?.hint = "222"
            btn?.alpha = 0.5f
            btn?.setLines(1)
        }
        // 不为null的条件下，才会去执行let函数体
        buttonUserInfoSubmit?.let {
            it.text = "111"
            it.hint = "222"
            it.alpha = 0.5f
            it.setLines(1)
        }
        //尝试自己实现一个
        fun <T, R> T.let1(block: (T) -> R): R {
            return block(this)
        }
        //2. with
        //this指代当前对象或者省略,适用于调用同一个类的多个方法时
        //闭包形式返回
        val userInfo = UserInfo()
        with(userInfo) {
            name = "ljy"
            age = 18
            email = "xxx@qq.com"
        }
        with(buttonUserInfoSubmit!!) {
            this.text = "111"
            this.hint = "222"
            this.alpha = 0.5f
            this.setLines(1)
        }
        with(buttonUserInfoSubmit) {
            this?.text = "111"
            this?.hint = "222"
            this?.alpha = 0.5f
            this?.setLines(1)
        }
        //尝试自己实现一个
        fun <T, R> with1(t: T, block: T.() -> R): R {
            return t.block()
        }

        //3. run
        //
        //是let和with两个函数的结合体，既能避免对象判空，又能this指代当前对象或者省略
        //闭包形式返回
        buttonUserInfoSubmit?.run {
            text = "111"
            hint = "222"
            alpha = 0.5f
            setLines(1)
        }
        //尝试自己实现一个HandlerThread
        fun <T, R> T.run1(block: T.() -> R): R {
            return block()
        }
        //4. apply
        //和run函数很像，this指代当前对象或者省略, 唯一不同是返回值为this
        buttonUserInfoSubmit?.apply {
            text = "111"
            hint = "222"
            alpha = 0.5f
            setLines(1)
        }
        //尝试自己实现一个
        fun <T> T.apply1(block: T.() -> Unit): T {
            block()
            return this
        }
        //5. also
        //和let很像，it指代当前对象， 唯一不同是返回值为this
        buttonUserInfoSubmit?.also {
            it.text = "111"
            it.hint = "222"
            it.alpha = 0.5f
            it.setLines(1)
        }
        //尝试自己实现一个
        fun <T> T.also1(block: (T) -> Unit): T {
            block(this)
            return this
        }


    }

    private fun doSomething() {

    }
}